// 在开始导入部分添加
import { createRouter, createWebHistory } from 'vue-router';
import Cookies from 'js-cookie';
import { ElMessage } from 'element-plus';

// 禁用浏览器缓存
const nocache = (to, from, next) => {
  // 添加元数据来禁用缓存
  if (to.matched.some(record => record.meta.noCache)) {
    to.meta.timestamp = new Date().getTime();
  }
  next();
};

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { noCache: true }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { noCache: true }
  },
  {
    path: '/accounts',
    name: 'Accounts',
    component: () => import('@/views/Accounts.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/transfer',
    name: 'Transfer',
    component: () => import('@/views/Transfer.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/ai-chat',
    name: 'AIChat',
    component: () => import('@/views/AIChat.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/bills',
    name: 'Bills',
    component: () => import('@/views/BillingDetails.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/products',
    name: 'Products',
    component: () => import('@/views/Products.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/loans',
    name: 'Loans',
    component: () => import('@/views/Loans.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/views/UnderDevelopment.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/test-products',
    name: 'TestProducts',
    component: () => import('@/views/test/ProductTest.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/security',
    name: 'SecuritySettings',
    component: () => import('@/views/SecuritySettings.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 全局前置导航守卫，禁用缓存
router.beforeEach(nocache);

// 解决页面切换时数据不刷新的问题
router.beforeEach((to, from, next) => {
  console.log(`Navigation: ${from.path} -> ${to.path}`);
  
  // 对于相同路径的导航，添加时间戳确保组件重新渲染
  if (to.path === from.path && to.fullPath === from.fullPath) {
    console.log('Same path navigation detected, adding timestamp');
    to.query = { ...to.query, _t: Date.now() };
  }
  
  next();
});

// 身份验证导航守卫  
router.beforeEach((to, from, next) => {
  // 获取token
  const token = Cookies.get('virtual_bank_token') || Cookies.get('token');
  
  console.log(`Router navigation: ${from.path} -> ${to.path}`);
  
  // 检查是否需要身份验证
  if (to.meta.requiresAuth) {
    // 如果需要身份验证，但没有token，则跳转到登录页面
    if (!token) {
    ElMessage.warning('请先登录');
    // 使用location.href强制刷新页面
      window.location.href = '/login';
    return; // 防止继续执行next()
    } else {
        next();
      }
  } else {
    // 如果已经有token，且尝试访问登录或注册页面，则跳转到首页
    if (token && (to.name === 'Login' || to.name === 'Register')) {
      next({ name: 'Home' });
    } else {
      next();
    }
  }
});

export default router;
